<template>
  <a-modal
    title="角色权限信息"
    :width="800"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }">
    <a-spin :spinning="loading">
      <a-form
        :form="form"
        v-bind="formLayout">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="角色名称">
              <a-input
                placeholder="请输入角色名称"
                v-decorator="['roleName', { rules: [{ required: true, message: '请输入角色名称' }] }]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="备注">
              <a-input
                placeholder="请输入备注"
                v-decorator="['remark']" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="权限资源">
              <a-tree-select
                v-if="menuTree"
                v-decorator="['menuIdList', { rules: [{ required: true, message: '请选择角色权限' }] }]"
                :tree-data="menuTree"
                tree-checkable
                :show-checked-strategy="SHOW_ALL"
                :replaceFields="{value: 'menuId',title:'name', key: 'menuId'}"
                placeholder="请选择角色权限"
                @check="onCheck"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { TreeSelect } from 'ant-design-vue'
// 表单字段
const fields = ['roleName', 'remark', 'menuIdList']
const SHOW_ALL = TreeSelect.SHOW_ALL

export default {
  name: 'Xxx',
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    menuTree: {
      type: Array,
      default: () => null
    }
  },
  components: {
    // TreeSelect
  },
  methods: {
    onCheck (checkedKeys, info) {
      console.log(checkedKeys, info)
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      form: this.$form.createForm(this),
      SHOW_ALL
    }
  },
  mounted () {
  },
  created () {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  }
}
</script>
